package com.maila.xiaoaoschool.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.adapter.OrderAdapter;
import com.maila.xiaoaoschool.widget.xlistview.XListView;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 订单详情
 * Created by Administrator on 2017/10/30.
 */

public class OrderActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.user_order_tabLayout)
    TabLayout mTabLayout;
    @BindView(R.id.user_order_vp)
    ViewPager userOrderVp;
    OrderAdapter orderAdapter;
    private LayoutInflater mInflater;
    private List<String> mTitleList = new ArrayList<>();
    private View view1, view2, view3, view4, view5;
    private List<View> mViewList = new ArrayList<>();
    ViewHolder vh1,vh2,vh3,vh4,vh5;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        ButterKnife.bind(this);
        orderAdapter=new OrderAdapter(this);
        initview();

        OrderPagerAdapter mAdapter = new OrderPagerAdapter(mViewList);
        userOrderVp.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(userOrderVp);
        mTabLayout.setTabsFromPagerAdapter(mAdapter);

    }

    private void initview() {
        mInflater = LayoutInflater.from(this);
        view1 = mInflater.inflate(R.layout.viewpager_order, null);
        view2 = mInflater.inflate(R.layout.viewpager_order, null);
        view3 = mInflater.inflate(R.layout.viewpager_order, null);
        view4 = mInflater.inflate(R.layout.viewpager_order, null);
        view5 = mInflater.inflate(R.layout.viewpager_order, null);
        vh1=new ViewHolder(view1);
        vh2=new ViewHolder(view2);
        vh3=new ViewHolder(view3);
        vh4=new ViewHolder(view4);
        vh5=new ViewHolder(view5);
        view1.setTag(vh1);
        view2.setTag(vh2);
        view3.setTag(vh3);
        view4.setTag(vh4);
        view5.setTag(vh5);

        vh1.lvContent.setAdapter(orderAdapter);
        vh2.lvContent.setAdapter(orderAdapter);
        vh3.lvContent.setAdapter(orderAdapter);
        vh4.lvContent.setAdapter(orderAdapter);
        //添加页卡视图
        mViewList.add(view1);
        mViewList.add(view2);
        mViewList.add(view3);
        mViewList.add(view4);
        mViewList.add(view5);

        //添加页卡标题
        mTitleList.add("全部");
        mTitleList.add("已完成");
        mTitleList.add("未完成");
        mTitleList.add("待评价");
        mTitleList.add("退款");


        mTabLayout.setTabMode(TabLayout.MODE_FIXED);//设置tab模式，当前为系统默认模式
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(0)));
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(1)));
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(2)));
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(3)));
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(4)));
    }

    @OnClick(R.id.white_return_iv)
    public void onViewClicked() {
        finish();
    }


    class OrderPagerAdapter extends PagerAdapter {
        private List<View> mViewList;

        public OrderPagerAdapter(List<View> mViewList) {
            this.mViewList = mViewList;
        }

        @Override
        public int getCount() {
            return mViewList.size();//页卡数
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;//官方推荐写法
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position));//添加页卡
            return mViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));//删除页卡
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);//页卡标题
        }

    }

    static class ViewHolder {
        @BindView(R.id.lv_content)
        XListView lvContent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
